!(function ($) {
  // 1.二级菜单

  const indexLeftNavBox = $(".indexLeftNav  .indexLeftNavBox ");
  const cartlist = $(".cartlist");
  const items = $(".item");

  indexLeftNavBox.hover(function () {
    $(this).addClass("active").siblings("li").removeClass("active");
    cartlist.show();
    items.eq($(this).index()).show().siblings(".item").hide();
  });

  cartlist.hover(
    function () {
      $(this).show();
    },
    function () {
      $(this).hide();
    }
  );

  //2.渲染列表
  const indexTvreboRightConent = $(".indexTvreboRightConent");
  $.ajax({
    url: "http://localhost/JS2112/dongfang/php/goodlist.php",
    dataType: "json",
  }).then(function (data) {
    console.log(data);
    let str = "";
    $.each(data, function (index, value) {
      str += ` <div class="indexTvrebosliderBoxFloatItem">
            <div class="hCommonItem">
              <div class="hItemShowImg">
                <a href=""><img src="${value.goods_img}" alt="" /></a>
              </div>
              <div class="hItemShowTitle">
                <a href="">${value.goods_title}</a>
              </div>
              <div class="hItemShowMoney">
                <span>${"￥" + value.goods_price}</span>
              </div>
            </div>
          </div>`;
    });

    $(".indexTvreboRightConent").html(str);
  });
})(jQuery);

// 轮播图
!(function ($) {
  const draggable = $(".draggable");
  const slickslide = $(".draggable div");
  const silderNav = $(".draggable  ul li");
  const slickprev = $(".slick-prev");
  const slicknext = $(".slick-next");
  let index = 0;
  let timer = null;

  silderNav.on("click", function () {
    index = $(this).index();
    tabSwitch();
  });

  draggable.hover(
    function () {
      slickprev.show();
      slicknext.show();

      clearInterval(timer);
    },
    function () {
      slickprev.hide();
      slicknext.hide();
      timer = setInterval(() => {
        slicknext.click();
      }, 3000);
    }
  );

  slicknext.on("click", function () {
    index++;
    if (index > silderNav.length - 1) {
      index = 0;
    }
    tabSwitch();
  });

  slickprev.on("click", function () {
    index--;
    if (index < 0) {
      index = silderNav.length - 1;
    }
    tabSwitch();
  });

  function tabSwitch() {
    silderNav
      .eq(index)
      .addClass("silderNav")
      .siblings("li")
      .removeClass("silderNav");
    slickslide
      .eq(index)
      .stop(true)
      .animate({ opacity: 1 })
      .siblings("div")
      .stop(true)
      .animate({ opacity: 0 });
  }

  timer = setInterval(() => {
    slicknext.click();
  }, 3000);
})(jQuery);
